package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.utils.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.AdvDevicerelationMapper;
import com.ruoyi.domain.AdvDevicerelation;
import com.ruoyi.service.IAdvDevicerelationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class AdvDevicerelationServiceImpl implements IAdvDevicerelationService
{
    @Autowired
    private AdvDevicerelationMapper advDevicerelationMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param advDevicerelationId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AdvDevicerelation selectAdvDevicerelationByAdvDevicerelationId(Long advDevicerelationId)
    {
        return advDevicerelationMapper.selectAdvDevicerelationByAdvDevicerelationId(advDevicerelationId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param advDevicerelation 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AdvDevicerelation> selectAdvDevicerelationList(AdvDevicerelation advDevicerelation)
    {
        return advDevicerelationMapper.selectAdvDevicerelationList(advDevicerelation);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param advDevicerelation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAdvDevicerelation(AdvDevicerelation advDevicerelation)
    {
        return advDevicerelationMapper.insertAdvDevicerelation(advDevicerelation);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param advDevicerelation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAdvDevicerelation(AdvDevicerelation advDevicerelation)
    {
        return advDevicerelationMapper.updateAdvDevicerelation(advDevicerelation);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param advDevicerelationIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdvDevicerelationByAdvDevicerelationIds(Long[] advDevicerelationIds)
    {
        try {
            return advDevicerelationMapper.deleteAdvDevicerelationByAdvDevicerelationIds(advDevicerelationIds);
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("请先删除关联数据");
        }
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param advDevicerelationId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdvDevicerelationByAdvDevicerelationId(Long advDevicerelationId)
    {
        try {
            return advDevicerelationMapper.deleteAdvDevicerelationByAdvDevicerelationId(advDevicerelationId);
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("请先删除关联数据");
        }
    }

    @Override
    @Transactional
    public String syncDevice(AdvDevicerelation advDevicerelation) {
        // 先删除所有的视频
        int num = advDevicerelationMapper.removeSync(advDevicerelation);
        // 然后添加视频
        String[] productsIds = advDevicerelation.getProductsIds();
        if(productsIds != null) {
            for (int i = 0; i < productsIds.length; i++) {
                AdvDevicerelation devicerelation = new AdvDevicerelation();
                devicerelation.setAdvMembersId(advDevicerelation.getAdvMembersId());
                devicerelation.setDevicesno(advDevicerelation.getDevicesno());
                devicerelation.setAdvProductsId(Long.valueOf(productsIds[i]));
                // 是否为默认播放视频 - 设置第一个为默认
                if(i==0) {
                    devicerelation.setIsChoosevideo("1");
                } else {
                    devicerelation.setIsChoosevideo("0");
                }
                advDevicerelationMapper.insertAdvDevicerelation(devicerelation);
            }
        }
        return "设置成功";
    }

    @Override
    @Transactional
    public boolean changeDefault(AdvDevicerelation advDevicerelation) {
        // 需要把当前设备的所有的内容都设置为不是默认播放
        int nums = advDevicerelationMapper.removeAllDefault(advDevicerelation.getDevicesno());
        // 然后设置上传的内容为默认播放
        int count = advDevicerelationMapper.setDefault(advDevicerelation);
        return count != 0;
    }

    @Override
    public String syncLive(AdvDevicerelation advDevicerelation) {
        advDevicerelation.setIsChoosevideo("0");
        int i = advDevicerelationMapper.insertAdvDevicerelation(advDevicerelation);
        return i==0?"同步失败":"同步成功";
    }

    @Override
    public String removeById(String devicesno, String advBroadcastId) {
        int i = advDevicerelationMapper.removeById(devicesno, advBroadcastId);
        return i==0?"取消同步成功":"取消同步失败";
    }

    @Override
    @Transactional
    public SimpleResult updateVoice(AdvDevicerelation advDevicerelation) {
        SimpleResult result = SimpleResult.error("未知错误");
        String label = advDevicerelation.getUpdateLabel();
        switch (label) {
            case "chooseaudio":
                if(advDevicerelation.getIsChooseaudio().equals("1")) {
                    int calcelRows = advDevicerelationMapper.calcelDefaultVoice(advDevicerelation);
                    if(calcelRows != 0) {
                        result = SimpleResult.success("取消默认成功");
                    } else {
                        result = SimpleResult.error("取消默认失败");
                    }
                } else {
                    // 1. 设置默认之前要取消其他的默认
                    int cancelRows = advDevicerelationMapper.cancelDefaultVoice(advDevicerelation);
                    int setRows = advDevicerelationMapper.setDefaultVoice(advDevicerelation);
                    if(setRows != 0) {
                        result = SimpleResult.success("设为默认成功");
                    } else {
                        result = SimpleResult.error("设为默认失败");
                    }
                }
                break;
            case "cycle":
                if(advDevicerelation.getIsCycle().equals("1")) {
                    int cycleRows = advDevicerelationMapper.cancelCycle(advDevicerelation);
                    if(cycleRows != 0) {
                        result = SimpleResult.success("取消循环成功");
                    } else {
                        result = SimpleResult.error("取消循环失败");
                    }
                } else {
                    int cycleRows = advDevicerelationMapper.setCycle(advDevicerelation);
                    if(cycleRows != 0) {
                        result = SimpleResult.success("循环成功");
                    } else {
                        result = SimpleResult.error("循环失败");
                    }
                }
                break;
            case "sync":
                if(advDevicerelation.getIsSync().equals("1")) {
                    // 表示是有取消同步的, 要查看当前设备是否是默认, 如果是默认, 就要重新设置一个默认
                    if(advDevicerelation.getIsChooseaudio().equals("1")) {
                        AdvDevicerelation devicerelation = new AdvDevicerelation();
                        devicerelation.setAdvMembersId(advDevicerelation.getAdvMembersId());
                        devicerelation.setDevicesno(advDevicerelation.getDevicesno());
                        List<AdvDevicerelation> advDevicerelations = advDevicerelationMapper.selectAdvDevicerelationList(devicerelation);
                        if(advDevicerelations.size() > 2) {
                            AdvDevicerelation devicerelation1 = advDevicerelations.get(1);
                            devicerelation1.setIsChooseaudio("1");
                            advDevicerelationMapper.updateAdvDevicerelation(devicerelation1);
                        }
                    }

                    int syncRows = advDevicerelationMapper.deleteVoice(advDevicerelation);
                    if(syncRows != 0) {
                        result = SimpleResult.success("取消同步成功");
                    } else {
                        result = SimpleResult.error("取消同步失败");
                    }
                } else {
                    // 表示是有加入同步的, 要查询是否有默认的内容, 如果没有默认的内容, 设置当前的内容为默认
                    AdvDevicerelation devicerelation = new AdvDevicerelation();
                    devicerelation.setAdvMembersId(advDevicerelation.getAdvMembersId());
                    devicerelation.setDevicesno(advDevicerelation.getDevicesno());
                    devicerelation.setIsChooseaudio("1");
                    List<AdvDevicerelation> advDevicerelations = advDevicerelationMapper.selectAdvDevicerelationList(devicerelation);
                    if(advDevicerelations.size() == 0) {
                        advDevicerelation.setIsChooseaudio("1");
                    }else {
                        advDevicerelation.setIsChooseaudio("0");
                    }

                    int syncRows = advDevicerelationMapper.syncVoice(advDevicerelation);
                    if(syncRows != 0) {
                        result = SimpleResult.success("同步成功");
                    }else {
                        result = SimpleResult.error("同步失败");
                    }
                }
                break;
        }
        return result;
    }
}
